package edu.uwm.RobberKnight.Controller;

import java.awt.Point;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.LinkedList;

import edu.uwm.RobberKnight.Model.Board;
import edu.uwm.RobberKnight.Model.GameState;
import edu.uwm.RobberKnight.Model.Player;
import edu.uwm.RobberKnight.Model.RobberKnightsGame;
import edu.uwm.RobberKnight.Model.Tile;

public class RobberKnightsController {

    private RobberKnightsGame _game;

    public RobberKnightsController() {
        newGame();
    }
    
	public boolean saveState(String fileName) {
		try {
			FileOutputStream fileOut = new FileOutputStream(fileName);
			ObjectOutputStream out = new ObjectOutputStream(fileOut);
			out.writeObject(_game);
			out.close();
			fileOut.close();
		} catch (IOException i) {
			return false;
		}
		return true;
	}
    
	public boolean loadState(String selFile) {
		RobberKnightsGame game = null;
		try {
			System.out.println("Trying to load game from " + selFile);
			FileInputStream fileIn = new FileInputStream(selFile);
			ObjectInputStream in = new ObjectInputStream(fileIn);
			game = (RobberKnightsGame) in.readObject();
			in.close();
			fileIn.close();
		} catch (Exception i) {
			System.out.println("Failed to read file: " + selFile);
			_game.print("Failed to load game.");
			return false;
		} 
		if (game == null)
			return false;
		else {
			_game = game;
		}
		return true;
    }

    public LinkedList<Tile> getTiles() {
        return _game.getBoard().getTiles();
    }

    public void newGame() {
        _game = new RobberKnightsGame();
    }

    public void addPlayer(String name, int age) {
        _game.addPlayer(name, age);
    }

    public LinkedList<Player> getPlayers() {
        return _game.getPlayers();
    }

    public int getBoardWidth() {
        return _game.getBoard().getWidth();
    }

    public int getBoardHeight() {
        return _game.getBoard().getHeight();
    }

    public void startGame() {
        _game.startGame();
    }

    public String[] getLogs() {
        return _game.getLogs();
    }

    public Player getCurrentPlayer() {
        return _game.getCurrentPlayer();
    }

    public LinkedList<Point> getValidTileLocs() {
        return _game.getBoard().getValidLocs();
    }

    public LinkedList<Tile> getValidKnightMoves() {
        return _game.getBoard().getValidMoves();
    }

    public int getMinKnight(Tile tile) {
        return _game.getBoard().getMinKnight(tile);
    }

    public void setSelectedTile(Tile tile) {
        _game.setSelectedTile(tile);
    }

    public boolean placeTile(int x, int y) {
    	return _game.placeTile(x, y);
    }
    
    public Tile getSelectedTile() {
        return _game.getSelectedTile();
    }
    
    public boolean placeKnight(Tile tile) {
        return tile.placeKnight(getCurrentPlayer(),getBoard());
    }

    public GameState getState() {
        return _game.getState();
    }

    public boolean nextTurn() {
        return _game.nextTurn();
    }

    public boolean isKnightsPlayable() {
        return _game.getBoard().isKnightsPlayable();
    }

    public Board getBoard() {
        return _game.getBoard();
    }
    
    public LinkedList<Player> getWinners() {
    	return _game.getWinners();
    }
    

}
